﻿using Autofac; 
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Linq;

namespace Developer.Core.FrameWork.AutoFacRegister
{
    public class AutofacModuleRegisterExtesions : Autofac.Module
    {
        private string[] _AssemblyArr;
        public AutofacModuleRegisterExtesions(string[] AssemblyArr)
        {
            this._AssemblyArr = AssemblyArr;
        }

        protected override void Load(ContainerBuilder builder)
        {
            var basePath = AppContext.BaseDirectory;
            var AopList = new List<Type>();

            // aop容器
            //builder.RegisterType<DeveloperCoreLogAOP>();
            //AopList.Add(typeof(DeveloperCoreLogAOP));



            foreach (string assembylyName in this._AssemblyArr)
            {
                var AssemblyPaths = Path.Combine(basePath, assembylyName);

                if (!File.Exists(AssemblyPaths))
                    throw new Exception("注入程序集不存在");

                var assemblyDllFile = Path.Combine(basePath, assembylyName);
                var assemblysRepository = Assembly.LoadFrom(assemblyDllFile);

                #region 程序集注入

                builder.RegisterAssemblyTypes(assemblysRepository).Where(x => !x.Namespace.Contains("Developer.Core.FrameWork.RedisExtensions.RedisSubscribeInit"))
                    .Where(x => !x.Namespace.Contains("Developer.Core.FrameWork.EventBus"))
                       .AsImplementedInterfaces()
                       .InstancePerDependency();
                       //.EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
                       //.InterceptedBy(AopList.ToArray());//允许将拦截器服务的列表分配给注册。

                #endregion

                #region 命名空间注入

                // 可以使用拦截器，但是拦截器的实现类必须是虚方法 
                //builder.RegisterAssemblyTypes(assemblysRepository).Where(x => x.Namespace == Appsettings.app("NameSpace")).AsImplementedInterfaces(); // 带有接口的注入
                //builder.RegisterAssemblyTypes(assemblysRepository).Where(x => x.Namespace == Appsettings.app("NameSpace")).EnableClassInterceptors(); // 具体类注入

                #endregion

                #region 根据类名/接口名关键字注入

                /*builder.RegisterAssemblyTypes(assemblysRepository).Where(x => x.Name.EndsWith(Appsettings.app("ClassNameKeyWord"))).AsImplementedInterfaces();
                builder.RegisterAssemblyTypes(assemblysRepository).Where(x => x.Name.EndsWith(Appsettings.app("ClassNameKeyWord"))).EnableClassInterceptors();*/

                #endregion

                #region 无接口单独类注入,启用class动态拦截，AOP动态代理

                //builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(Appsettings)))
                //    .EnableClassInterceptors()
                //    .InterceptedBy(AopList.ToArray());

                #endregion

                #region 有接口单独类注入

                //builder.RegisterType<Service>().As<IService>()
                //    .AsImplementedInterfaces()
                //    .EnableInterfaceInterceptors()
                //    .InterceptedBy(AopList);

                #endregion
            } 
        }
    }
}
